//
//  XOGoodsCartListView.m
//  didigo
//
//  Created by JS_Coder on 2019/2/17.
//  Copyright © 2019 lifayu. All rights reserved.
//

#import "XOGoodsCartListView.h"
#import "XOShoppingCartModel.h"
#import "EJSShopCartGoodsCell.h"
#import "XOShoppingCartListModel.h"
#import "EJSUpdateShopCartRequest.h"
#import "JSONKit.h"
#import "XOShoppingCartListRequest.h"
#import "XOShopViewModel.h"
#import "EJSShoppingCarSingleton.h"

@interface XOGoodsCartListView()<UITableViewDelegate, UITableViewDataSource, EJSShopCartGoodsCellDelegate>
@property(nonatomic, strong) XOShoppingCartModel * shopCartModel;
@property (weak, nonatomic) IBOutlet UIImageView *shopImageView;
@property(nonatomic, strong) XOShoppingCartListModel * shopListModel;

@end

@implementation XOGoodsCartListView

- (void)awakeFromNib {
    [super awakeFromNib];
    [_tableView registerNib:[UINib nibWithNibName:@"EJSShopCartGoodsCell" bundle:nil] forCellReuseIdentifier:@"EJSShopCartGoodsCell"];
    _tableView.rowHeight = 120;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti_updateShoppingCarList) name:Noti_UpdateShoppingCarList object:nil];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setShopCartModel:(XOShoppingCartModel *)shopCartModel{
    _shopCartModel = shopCartModel;
    NSMutableArray *tempArray = [NSMutableArray new];
    for (EJSShoppingCartGoodsModel *goodModel in shopCartModel.carts) {
        if ([goodModel.spuId isEqualToString: [XOShopViewModel shared].spuId]) {
            [tempArray addObject:goodModel];
        }
    }
    [XOShopViewModel shared].shopCartGoodsList = tempArray;
    [_shopImageView setImageWithUrl:shopCartModel.logo];
    [_tableView reloadData];
}


+(XOGoodsCartListView *)showSelectGoodsCartView:(XOShoppingCartModel *)shopCartModel withCartList: (XOShoppingCartListModel *) listModel{
    XOGoodsCartListView *cartView = [[UINib nibWithNibName:@"XOGoodsCartListView" bundle:nil] instantiateWithOwner:self options:nil].firstObject;
    cartView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    cartView.shopCartModel = shopCartModel;
    cartView.shopListModel = listModel;
    UIWindow *kewWindow = [EJSAppDelegate sharedAppDelegate].window;
    [kewWindow addSubview:cartView];
    return cartView;
}

- (IBAction)dissMissButtonAction:(UIButton *)sender {

    if ([self.delegate respondsToSelector:@selector(shoppingCartViewWillDisapear)]) {
        [self.delegate shoppingCartViewWillDisapear];
    }
    [self removeFromSuperview];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [XOShopViewModel shared].shopCartGoodsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EJSShopCartGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSShopCartGoodsCell" forIndexPath:indexPath];
    EJSShoppingCartGoodsModel* goodModel = [XOShopViewModel shared].shopCartGoodsList[indexPath.row];
    [cell setShoppingGoodsCartModel: goodModel];
    cell.delegate = self;
//    [cell.selectButton setHidden:YES];
    return cell;
}

- (void)shopCartGoodsCell:(EJSShopCartGoodsCell *)cell addGoodsNum:(EJSShoppingCartGoodsModel *)model {
    NSInteger selectNum = model.num.intValue;
    model.num = @(selectNum + 1).stringValue;
    [self updateShopCartRequest];
}

- (void)shopCartGoodsCell:(EJSShopCartGoodsCell *)cell substractGoodsNum:(EJSShoppingCartGoodsModel *)model {
    NSInteger selectNum = model.num.intValue;
    NSInteger minToBuy = model.minToBuy.intValue;
    if(selectNum - minToBuy > 0){
        model.num = @(selectNum - minToBuy).stringValue;
        [self updateShopCartRequest];
    }else if (selectNum - minToBuy <= 0){
        [self shopCartGoodsCell:cell deleteGoods:model];
    }
}

- (void)shopCartGoodsCell:(EJSShopCartGoodsCell *)cell deleteGoods:(EJSShoppingCartGoodsModel *)model {
    [HUDHelper alertTitle:@"提示" message:@"是否删除购物车中的该商品" cancelButtonTitle:@"取消" confirmButtonTitle:@"确认" action:^{
        [self.shopListModel deleteGoods:model];

        [self updateShopCartRequest];
    }];
}

- (void)updateShopCartRequest{
    NSMutableArray *datas = [NSMutableArray array];
    for (EJSShoppingCartGoodsModel *goodsModel in self.shopListModel.wholeGoodsList) {
        EJSUpdateShopCartModel *model = [EJSUpdateShopCartModel new];
        model.cart_id = goodsModel.cartId;
        model.sku_id = goodsModel.spuId;
        model.num = goodsModel.num;
        [datas addObject:[model getJsonDictionary]];
    }
    
    EJSUpdateShopCartRequest *req = [EJSUpdateShopCartRequest new];
    req.data = [datas JSONString];
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        [self loadShoppingCartList];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

#pragma Load CartList
- (void)loadShoppingCartList{
    if(![EJSUserTokenModel userTokenModel]){
        return;
    }
    FlyWeakSelf
    [[EJSShoppingCarSingleton sharedSingleton]updateShoppingList:^(XOShoppingCartListModel * modelList) {
        [weakSelf noti_updateShoppingCarList];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
    
}

-(void)noti_updateShoppingCarList{
    self.shopListModel = [EJSShoppingCarSingleton sharedSingleton].shoppingList;
    int hasCount = 0;
    if(self.shopListModel.list.count > 0){
        for (XOShoppingCartModel* model in self.shopListModel.list) {
            if (model.shopId == self.shopCartModel.shopId) {
                    self.shopCartModel = model;
                    hasCount += 1;
                }
        }
        if (hasCount == 0) {
                self.shopCartModel = nil;
                [[HUDHelper sharedInstance] tipMessage: @"已无任何商品"];
        }
    }else{
        [[HUDHelper sharedInstance] tipMessage: @"已无任何商品"];
    }
}
@end
